package com.lagou.http;

import com.lagou.chat.NettyChatServer;
import com.lagou.chat.NettyChatServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * http服务端
 */
public class NettyHttpServer {

    private int port;

    public NettyHttpServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws InterruptedException {
        NettyHttpServer nettyHttpServer = new NettyHttpServer(8080);
        nettyHttpServer.run();
    }

    public void run() throws InterruptedException {
        //1. 创建bossGroup线程组: 处理网络事件--连接事件,不设置线程数默认为: 2*处理器线程数
        NioEventLoopGroup bossGroup = null;
        //2. 创建workerGroup线程组: 处理网络事件--读写事件,不设置线程数默认为: 2*处理器线程数
        NioEventLoopGroup workerGroup = null;
        try {
            bossGroup = new NioEventLoopGroup(1);
            workerGroup = new NioEventLoopGroup();
            //3. 创建服务端启动助手
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            //4. 设置bossGroup线程组和workerGroup线程组
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)//5. 设置服务端通道实现为NIO
                    .option(ChannelOption.SO_BACKLOG, 128)//6. 参数设置-TCP/IP协议listen函数中的backlog参数，来初始化服务器可连接队列大小
                    .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)//7. 参数设置-设置活跃状态,child是设置workerGroup
                    .childHandler(new ChannelInitializer<SocketChannel>() {//8. 创建一个通道初始化对象
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            //8. 向pipeline中添加自定义业务处理handler
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            // 加入编解码器,这里咱们是用http编解码器
                            pipeline.addLast(new HttpServerCodec());
                            //加入业务处理器
                            pipeline.addLast(new NettyHttpServerHandler());
                        }
                    });
            //9. 启动服务端并绑定端口,同时将异步改为同步
            ChannelFuture future = serverBootstrap.bind(port);
            future.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
                        System.out.println("绑定端口成功");
                    } else {
                        System.out.println("绑定端口失败");
                    }
                }
            });
            System.out.println("=========服务端启动成功=========");
            future.channel().closeFuture().sync();
        } finally {
            //10. 关闭通道(并不是真正的关闭,而是一直监听通道关闭状态)和关闭连接池
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

}
